import java.util.ArrayList;
import java.util.List;

/**
 * @program: LeetCode
 * @description: LeetCode : 39. 组合总和
 * @author: WXY
 * @create: 2023-01-09 11:13
 * @Version 1.0
 **/
public class Num039_combinationSum {
    public static List<List<Integer>> combinationSum(int[] arr, int aim) {
        if (arr.length == 0 || arr == null || aim < 0) return new ArrayList<>();
        List<List<Integer>> list = new ArrayList<>();
        ArrayList<Integer> curList = new ArrayList<>();
        process1(arr, 0, aim, curList, list);
        return list;
    }

    private static void process1(int[] arr, int index, int rest, ArrayList<Integer> curList, List<List<Integer>> list) {
        if (index == arr.length) {
            if (rest == 0) {
                list.add(new ArrayList<>(curList));
            }
            return;
        }
        for (int i = index; i < candidates.length && curSum + candidates[i] <= target; i++) {
            curList.add(candidates[i]);
            curSum += candidates[i];
            process(candidates, target, i , curSum);
            curSum -= candidates[i];
            curList.remove(curList.size() - 1);
        }
    }

    public static void main(String[] args) {
        int[] arr = {2, 3, 6, 7};
        List<List<Integer>> lists = combinationSum(arr, 7);
        System.out.println(lists);
    }
}
